package com.tihi.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

/**
 * 该类比CircleImageView性能好
 * @author thduan
 * 2014-12-17 11:17:28
 */
public class CircleImageView2 extends AbsShapeImageView{

	/** 透明度 */
	private int opaque = 255;
	
	public CircleImageView2(Context context) {
		super(context, null, 0);
	}
	
	public CircleImageView2(Context context, AttributeSet attrs) {
		super(context, attrs, 0);
	}
	
	public CircleImageView2(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void drawCustomShape(Canvas canvas, RectF rectF, Paint paint) {
		paint.setAlpha(opaque);
		float radius = Math.min(rectF.width() / 2, rectF.height() / 2);
		//ps: cx、cy必须取 getWidth/2、getHeight/2，不能使用 rectF的中心
		canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);
	}
	
	public void setAlphaValue(int alpha) {
		opaque = alpha;
		invalidate();
	}

	@Override
	protected void drawCustomBorder(Canvas canvas, RectF borderRectF, float borderWidth,
			Paint borderPaint) {
		float radius = Math.min((borderRectF.width() - borderWidth) / 2, (borderRectF.height() - borderWidth) / 2);
		canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, borderPaint);
	}

}
